/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.beansjsf.tutoriais;

import br.com.saber.certificacao.beansjsf.interfacebean.BeanJSFInterface;
import br.com.saber.certificacao.beansjsf.login.LoginManager;
import br.com.saber.certificacao.beansjsf.tutoriais.lazytable.LazyTutorialModel;
import br.com.saber.certificacao.ejbs.stateless.interfaces.TutorialBeanLocal;
import br.com.saber.certificacao.modelos.Tutorial;
import br.com.saber.certificacao.modelos.validadores.exceptions.generica.ValidacaoExcecao;
import br.com.saber.certificacao.utils.MensagemUtils;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.lang.StringUtils;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author Guilherme
 */
@ManagedBean(name = "tutorialManager")
@ViewScoped
public class TutorialManager implements BeanJSFInterface {

    @EJB
    private TutorialBeanLocal tutorialBean;
    
    @ManagedProperty("#{loginManager}")
    private LoginManager loginManager;
    
    public Tutorial tutorialCadastro;

    private LazyDataModel<Tutorial> tutoriais;
    
    public TutorialManager() {
    }
    //====================
    // Iniciadores        
    //====================

    @Override
    @PreDestroy
    public void end() {
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "TutorialManager.end() executado!");
    }

    @Override
    @PostConstruct
    public void init() {
        tutoriais = new LazyTutorialModel(tutorialBean);
        clean();
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "TutorialManager.init() executado!");
    }

    //====================
    //Métodos de Negócio  
    //====================
    public void clean() {
        this.tutorialCadastro = new Tutorial();
        this.tutorialCadastro.setUsuario(this.loginManager.getUsuario());
    }

    public void salvarTutorial() {
        try {
            this.tutorialBean.salvarTutorial(tutorialCadastro);
            MensagemUtils.messageFactoringFull("tutorialSalvoOK", new String[]{tutorialCadastro.getTituloTutorial()}, FacesMessage.SEVERITY_INFO, FacesContext.getCurrentInstance());
            clean();
        } catch (ValidacaoExcecao v) {
            if (!StringUtils.isBlank(v.getAtributoName())) {
                MensagemUtils.messageFactoringFull(v.getAtributoName(), null, v.getMessage(), null, FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
            } else {
                MensagemUtils.messageFactoringFull(v.getMessage(), null, FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
            }
        }
    }
    //====================
    //Table Actions       
    //====================
    //====================
    //SelectItem          
    //====================
    //====================
    //Getters AND Setters 
    //====================

    public Tutorial getTutorialCadastro() {
        return tutorialCadastro;
    }

    public void setTutorialCadastro(Tutorial tutorialCadastro) {
        this.tutorialCadastro = tutorialCadastro;
    }

    public LoginManager getLoginManager() {
        return loginManager;
    }

    public void setLoginManager(LoginManager loginManager) {
        this.loginManager = loginManager;
    }

    public LazyDataModel<Tutorial> getTutoriais() {
        return tutoriais;
    }

    public void setTutoriais(LazyDataModel<Tutorial> tutoriais) {
        this.tutoriais = tutoriais;
    }
}
